import { createRouter, createWebHistory } from 'vue-router'
import AdminLayout from '@/layouts/admin/AdminLayout.vue'
import HomeView from '@/views/HomeView.vue'
import LoginView from '@/views/LoginView.vue'
import ProfileView from '@/views/ProfileView.vue'
import AdminIndex from '@/views/admin/AdminIndex.vue'
import AdminManage from '@/views/admin/AdminManage.vue'
import UserManage from '@/views/admin/UserManage.vue'

// 首页内容组件 - 欢迎页面
const HomeContent = {
  template: `<div>
    <h1>欢迎使用通用开发模板</h1>
    <p>这是一个功能齐全的开发模板，包含用户管理、权限控制等功能。</p>
  </div>`
}

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/',
      name: 'home',
      component: HomeView,
      meta: {
        title: '首页',
      },
      children: [
        {
          // 默认显示欢迎页
          path: '',
          name: 'homeContent',
          component: HomeContent
        },
        {
          // 个人中心作为首页的子路由
          path: 'profile',
          name: 'userProfile',
          component: ProfileView,
          meta: {
            title: '个人中心',
            requireAuth: true,
          }
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: LoginView,
      meta: {
        title: '登录',
      }
    },
    {
      path: '/admin',
      name: 'admin',
      component: AdminLayout,
      meta: {
        title: '管理后台',
        requireAuth: true,
        requireAdmin: true,
      },
      redirect: '/admin/index',
      children: [
        {
          path: '/admin/index',
          name: 'adminIndex',
          component: AdminIndex,
          meta: {
            title: '管理后台首页',
            requireAuth: true,
            requireAdmin: true,
          }
        },
        {
          path: '/admin/manage',
          name: 'adminManage',
          component: AdminManage,
          meta: {
            title: '用户管理',
            requireAuth: true,
            requireAdmin: true,
          }
        },
        {
          path: '/admin/profile',
          name: 'adminProfile',
          component: ProfileView,
          meta: {
            title: '个人中心',
            requireAuth: true,
            requireAdmin: true,
          }
        },
        {
          path: '/admin/users',
          name: 'userManage',
          component: UserManage,
          meta: {
            title: '用户管理',
            requireAuth: true,
            requireAdmin: true,
          }
        },
      ],
    },
    {
      path: '/403',
      name: '403',
      component: () => import('@/views/error/403.vue'),
      meta: {
        title: '403 - 无权限访问',
      }
    },
    {
      path: '/404',
      name: '404',
      component: () => import('@/views/error/404.vue'),
      meta: {
        title: '404 - 页面不存在',
      }
    },
    // 所有未匹配到的路由都将重定向到404页面
    {
      path: '/:pathMatch(.*)*',
      redirect: '/404',
    },
  ],
})

export default router
